package cn.nfwx.tv.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import cn.nfwx.tv.events.Event;
import cn.nfwx.tv.helper.UmengPushHelper;
import cn.nfwx.tv.model.UpdateModel;
import cn.nfwx.tv.model.User;
import cn.nfwx.tv.model.UserModel;

/**
 * Created by zb.liu on 2017/11/17.
 */

public class SharePrefUtil {

    /***
     * 保存用户数据
     * @param context
     * @param userModel
     */
    public static void saveUser(Context context, UserModel userModel) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (userModel != null) {
            editor.putString("access_token", userModel.getAccess_token());
            editor.putString("expires_in", userModel.getExpires_in());
            editor.putString("refresh_token", userModel.getRefresh_token());
            editor.putString("token_type", userModel.getToken_type());
            editor.putString("accountId", userModel.getUser().getAccountId());
            editor.putString("nickName", userModel.getUser().getNickName());
            editor.putString("phone", userModel.getUser().getPhone());
            editor.putString("salt", userModel.getUser().getSalt());
            editor.putString("logo", userModel.getUser().getLogo());
            editor.putString("shouldBindPhone", userModel.getUser().getShouldBindPhone());
        }

        editor.commit();


        try {
            // 登入事件
            LogUtil.e("handlerEvent", "postLoginEvent");
            Event.postLoginEvent();
            //AppointmentService.actionStart(NFWXApplication.getInstance());
            // 更新友盟信息
            UmengPushHelper.getInstance().addAccountId(getUid(context));
            UmengPushHelper.getInstance().upPushId(context);

            // 人员登入统计
            MobclickAgent.onProfileSignIn(getUid(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @param context
     * @param user
     */
    public static void saveIjkUser(Context context, User user) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("ijk_user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (user != null) {
            editor.putString("user_wxid", user.getWxid());
            editor.putString("user_logo", user.getLogo());
            editor.putString("user_mobile", user.getMobile());
            editor.putString("user_nick", user.getNick());
            editor.putString("user_token", user.getToken());
            editor.putInt("user_uid", user.getUid());
        }

        editor.commit();

        try {
            // 更新友盟信息
            UmengPushHelper.getInstance().addAccountId(getUid(context));
            // 人员登入统计
            MobclickAgent.onProfileSignIn(getIjkUser(context).getUid() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 获取用户
     * @param context
     * @return
     */
    public static UserModel getUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        UserModel userModel = new UserModel();
        if (sharedPreferences != null) {
            userModel.setAccess_token(sharedPreferences.getString("user_logo", null));
            userModel.setExpires_in(sharedPreferences.getString("expires_in", null));
            userModel.setRefresh_token(sharedPreferences.getString("refresh_token", null));
            userModel.setToken_type(sharedPreferences.getString("token_type", null));
            userModel.setExpires_in(sharedPreferences.getString("expires_in", null));
            UserModel.User user = new UserModel.User();
            user.setAccountId(sharedPreferences.getString("accountId", null));
            user.setNickName(sharedPreferences.getString("nickName", null));
            user.setPhone(sharedPreferences.getString("phone", null));
            user.setSalt(sharedPreferences.getString("salt", null));
            user.setLogo(sharedPreferences.getString("logo", null));
            user.setShouldBindPhone(sharedPreferences.getString("shouldBindPhone", null));
            userModel.setUser(user);

        }
        return userModel;
    }

    /***
     * 获取用户
     * @param context
     * @return
     */
    public static User getIjkUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("ijk_user", Context.MODE_PRIVATE);
        User userModel = new User();
        if (sharedPreferences != null) {
            userModel.setWxid(sharedPreferences.getString("user_wxid", null));
            userModel.setLogo(sharedPreferences.getString("user_logo", null));
            userModel.setMobile(sharedPreferences.getString("user_mobile", null));
            userModel.setNick(sharedPreferences.getString("user_nick", null));
            userModel.setToken(sharedPreferences.getString("user_token", null));
            userModel.setUid(sharedPreferences.getInt("user_uid", 0));
        }
        return userModel;
    }

    /***
     * 清除用户
     * @param context
     */
    public static void clearIjkUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("ijk_user", Context.MODE_PRIVATE);
        sharedPreferences.edit().clear().commit();
        // 人员登出统计
        MobclickAgent.onProfileSignOff();
    }


    /***
     * 清除用户
     * @param context
     */
    public static void clearUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        sharedPreferences.edit().clear().commit();
        // 登出event
        Event.postLogoutEvent();

        // 人员登出统计
        MobclickAgent.onProfileSignOff();
    }

    public static void saveWechatUser(Context context, UserModel userModel) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("wechatuser", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (userModel != null) {
            editor.putString("access_token", userModel.getAccess_token());
            editor.putString("expires_in", userModel.getExpires_in());
            editor.putString("refresh_token", userModel.getRefresh_token());
            editor.putString("token_type", userModel.getToken_type());
            editor.putString("accountId", userModel.getUser().getAccountId());
            editor.putString("nickName", userModel.getUser().getNickName());
            editor.putString("phone", userModel.getUser().getPhone());
            editor.putString("salt", userModel.getUser().getSalt());
            editor.putString("logo", userModel.getUser().getLogo());
            editor.putString("shouldBindPhone", userModel.getUser().getShouldBindPhone());
        }

        editor.apply();

    }

    /***
     * 清除微信用户
     * @param context
     */
    public static void clearWechatUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("wechatuser", Context.MODE_PRIVATE);
        sharedPreferences.edit().clear().apply();
    }

    public static UserModel getWechatUser(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("wechatuser", Context.MODE_PRIVATE);
        UserModel userModel = new UserModel();
        if (sharedPreferences != null) {

            userModel.setAccess_token(sharedPreferences.getString("access_token", null));
            userModel.setExpires_in(sharedPreferences.getString("expires_in", null));
            userModel.setRefresh_token(sharedPreferences.getString("refresh_token", null));
            userModel.setToken_type(sharedPreferences.getString("token_type", null));
            userModel.setExpires_in(sharedPreferences.getString("expires_in", null));
            UserModel.User user = new UserModel.User();
            user.setAccountId(sharedPreferences.getString("accountId", null));
            user.setNickName(sharedPreferences.getString("nickName", null));
            user.setPhone(sharedPreferences.getString("phone", null));
            user.setSalt(sharedPreferences.getString("salt", null));
            user.setLogo(sharedPreferences.getString("logo", null));
            user.setShouldBindPhone(sharedPreferences.getString("shouldBindPhone", null));
            userModel.setUser(user);

        }
        return userModel;
    }

    public static void saveFristStart(Context context, boolean isFrist) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("frist_start", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("is_frist_start", isFrist);
        editor.apply();
    }

    public static boolean getFristStart(Context context) {
        boolean isFrist = true;
        SharedPreferences sharedPreferences = context.getSharedPreferences("frist_start", Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            isFrist = sharedPreferences.getBoolean("is_frist_start", true);
        }
        return isFrist;
    }

    /**
     * 保存当前的position
     *
     * @param context
     * @papositionram
     */
    public static void saveLiveId(Context context, String lid) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(LIVEPOSITION, lid);
        editor.apply();
    }

    /**
     * 获取当前position
     *
     * @param context
     * @return
     */
    public static String getLiveId(Context context) {
        String lid = null;
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            lid = sharedPreferences.getString(LIVEPOSITION, "");
        }
        return lid;
    }

    /**
     * 保存当前的position
     *
     * @param context
     * @papositionram
     */
    public static void saveIjkLiveId(Context context, String lid) {
        if(context != null){
            SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_IJK_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(LIVEPOSITION, lid);
            editor.apply();
        }
    }

    /**
     * 获取当前position
     *
     * @param context
     * @return
     */
    public static String getIjkLiveId(Context context) {
        String lid = null;
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_IJK_NAME, Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            lid = sharedPreferences.getString(LIVEPOSITION, "");
        }
        return lid;
    }

    /**
     * 获取4g状态
     *
     * @param context
     * @return
     */
    public static boolean get4gstautus(Context context) {
        boolean isFrist = false;
        SharedPreferences sharedPreferences = context.getSharedPreferences("4g_status", Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            isFrist = sharedPreferences.getBoolean("4g_status", false);
        }
        return isFrist;
    }

    /**
     * 保存4g状态
     *
     * @param context
     * @param status
     */
    public static void save4gstautus(Context context, boolean status) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("4g_status", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("4g_status", status);
        editor.apply();
    }

    /**
     * 保存第一次介绍
     *
     * @param context
     * @param isFrist
     */
    public static void saveFristIntroduction(Context context, boolean isFrist) {
        if (context != null) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("firsh_introduction", Context.MODE_PRIVATE);
            if (sharedPreferences != null) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                if (editor != null) {
                    editor.putBoolean("is_frist_introduction", isFrist);
                    editor.apply();
                }
            }
        }
    }

    /**
     * 获取第一次介绍
     *
     * @param context
     */
    public static boolean getFristIntroduction(Context context) {
        boolean isFrist = true;
        SharedPreferences sharedPreferences = context.getSharedPreferences("firsh_introduction", Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            isFrist = sharedPreferences.getBoolean("is_frist_introduction", true);
        }
        return isFrist;
    }

    /**
     * 获取AccessToken
     *
     * @param context
     * @return
     */
    public static String getAccessToken(Context context) {
        UserModel userModel = getUser(context);
        if (userModel != null) {
            if (TextUtils.isEmpty(userModel.getAccess_token())) {
                return "";
            }
            return userModel.getAccess_token();
        } else {
            return "";
        }
    }

    /**
     * 获取用户id
     *
     * @param context
     * @return
     */
    public static String getUid(Context context) {
        UserModel userModel = getUser(context);
        if (userModel != null) {
            return userModel.getUser().getAccountId();
        } else {
            return "";
        }
    }

    /**
     * 获取用户名
     *
     * @param context
     * @return
     */
    public static String getUserName(Context context) {
        UserModel userModel = getUser(context);
        if (userModel != null) {
            return userModel.getUser().getNickName();
        } else {
            return "";
        }
    }

    private static final String PREFERENCE_NAME = "nfwx_lid";
    private static final String PREFERENCE_IJK_NAME = "preference_ijk_name";
    private static final String PREFERENCE_KEY_KEYWORD = "preference_key_keyword_1";
    private static final String LIVEPOSITION = "livePosition";
    private static final String QUIT_NOTICE = "quit_notice";
    private static final String LIVESTR = "livestr";

    /**
     * 存储查找关键字
     *
     * @param context
     * @param keyword
     */
    public static void saveSearchKeyWord(@NonNull Context context, String keyword) {
        if (TextUtils.isEmpty(keyword)) {
            return;
        }
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String keywords = sharedPreferences.getString(PREFERENCE_KEY_KEYWORD, "");
        ArrayList<String> list;
        if (TextUtils.isEmpty(keywords)) {
            list = new ArrayList<>();
        } else {
            list = JSONObject.parseObject(keywords, new TypeReference<ArrayList<String>>() {
            }.getType());
        }
        if (list.contains(keyword)) {
            list.remove(keyword);
        }
        list.add(0, keyword);
        editor.putString(PREFERENCE_KEY_KEYWORD, JSONObject.toJSONString(list));
        editor.apply();
    }

    /**
     * 获取查找关键字
     *
     * @param context
     * @return
     */
    public static ArrayList<String> getSearchKeyWord(Context context) {
        if (context == null) {
            return new ArrayList<>();
        }
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        String keywords = sharedPreferences.getString(PREFERENCE_KEY_KEYWORD, null);
        if (TextUtils.isEmpty(keywords)) {
            return null;
        }
        return JSONObject.parseObject(keywords, new TypeToken<ArrayList<String>>() {
        }.getType());
    }

    /**
     * 清除数据
     *
     * @param context
     */
    public static void clearSearchKeyWord(Context context) {
        if (context == null) {
            return;
        }
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PREFERENCE_KEY_KEYWORD, "");
        editor.apply();
    }

    /***
     * 保存推送设置选择
     * @param context
     * @param isPush
     */
    public static void savePushChoose(Context context, boolean isPush) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("push", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isPush", isPush);
        editor.commit();

        Event.postPullChangeEvent();
    }

    /***
     * 获取用户推送选择
     * @param context
     * @return
     */
    public static boolean getPushChoose(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("push", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("isPush", true);
    }

    /***
     * 保存接口返回的版本号
     * @param context
     * @param
     */
    public static void saveVersion(Context context, UpdateModel model) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("version", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("version", model.getVersionCode());
        editor.putString("message", model.getMessage());
        editor.putString("download", model.getDownload());
        editor.putString("force", model.getForce());

        editor.apply();
    }

    /***
     * 获取接口返回的版本号
     * @param context
     * @return
     */
    public static UpdateModel getVersion(Context context) {
        UpdateModel model = new UpdateModel();
        SharedPreferences sharedPreferences = context.getSharedPreferences("version", Context.MODE_PRIVATE);
        model.setVersionCode(sharedPreferences.getInt("version", 1));
        model.setDownload(sharedPreferences.getString("download", ""));
        model.setMessage(sharedPreferences.getString("message", ""));
        model.setForce(sharedPreferences.getString("force", "false"));
        return model;
    }

    /***
     * 保存下载设置选择
     * @param context
     * @param isDown
     */
    public static void saveDownloadChoose(Context context, boolean isDown) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("download", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isDown", isDown);
        editor.apply();
    }

    /***
     * 获取下载设置选择
     * @param context
     * @return
     */
    public static boolean getDownloadChoose(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("download", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("isDown", false);
    }

    /**
     * 保存启动图广告路径
     */
    public static void saveAdvertiseStr(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 获取广告启动图路径
     *
     * @param context
     * @param key
     * @return
     */
    public static String getAdvertiseStr(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    /**
     * 记录时长
     *
     * @return
     */
    public static void saveAdvertiseInt(Context context, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * 获取记录
     * @return
     */
    public static int getAdvertiseInt(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key,-1);
    }

    /**
     * @param context
     * @param key
     * @param value
     */
    public static void saveAdvertiseLocal(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 获取广告启动图路径
     *
     * @param context
     * @param key
     * @return
     */
    public static String getAdvertiseLocal(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("advertise", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    /**
     * 是否第一次提示
     *
     * @param context
     * @return
     */
    public static boolean getQuitNotice(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(QUIT_NOTICE, true);
    }

    /**
     * 设置显示过了
     *
     * @param context
     * @param state
     * @return
     */
    public static void setQuitNotice(Context context, boolean state) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(QUIT_NOTICE, state);
        editor.apply();
    }

    /**
     * 存放信息
     *
     * @param context
     * @param liveStr
     */
    public static void saveLiveStr(Context context, String liveStr) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(LIVESTR, liveStr);
        editor.apply();
    }

    /**
     * 获取直播信息
     *
     * @param context
     * @return
     */
    public static String getLivestr(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        return sharedPreferences.getString(LIVESTR, "");
    }
}
